﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Data;
using AzureTracer.Common;
using System.Linq;

namespace AzureTracer.Views.Controls
{
    public class ProgressIndicatorProxy : FrameworkElement
    {
        bool _isLoaded;

        #region DependencyProperties
        public bool IsIndeterminate
        {
            get { return (bool)GetValue(IsIndeterminateProperty); }
            set { SetValue(IsIndeterminateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsIndeterminate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsIndeterminateProperty =
            DependencyProperty.Register("IsIndeterminate", typeof(bool), typeof(ProgressIndicatorProxy), new PropertyMetadata(false));

        public bool IsVisible
        {
            get { return (bool)GetValue(IsVisibleProperty); }
            set { SetValue(IsVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.Register("IsVisible", typeof(bool), typeof(ProgressIndicatorProxy), new PropertyMetadata(true));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ProgressIndicatorProxy), new PropertyMetadata(string.Empty));

        #endregion

        public ProgressIndicatorProxy()
        {
            Loaded += OnLoaded;
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (_isLoaded) return;

            Attach();
            _isLoaded = true;
        }

        private void Attach()
        {
            if (DesignerProperties.IsInDesignTool) return;

            var page = this.GetVisualAncestors<PhoneApplicationPage>().First();

            var progressIndicator = SystemTray.ProgressIndicator;
            if (progressIndicator != null)
                return;

            progressIndicator = new ProgressIndicator();
            SystemTray.SetProgressIndicator(page, progressIndicator);

            Binding binding = new Binding("IsIndeterminate") { Source = this };
            BindingOperations.SetBinding(
                progressIndicator, ProgressIndicator.IsIndeterminateProperty, binding);

            binding = new Binding("IsVisible") { Source = this };
            BindingOperations.SetBinding(
                progressIndicator, ProgressIndicator.IsVisibleProperty, binding);

            binding = new Binding("Text") { Source = this };
            BindingOperations.SetBinding(
                progressIndicator, ProgressIndicator.TextProperty, binding);            
            
        }
    }
}
